package com.example.distinguishcolors.adapter;

import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.MaskFilterSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.distinguishcolors.R;
import com.example.distinguishcolors.domain.GameRecord;
import com.example.distinguishcolors.utils.DateUtils;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/10/3 22:35
 * @version 1.0
 */
public class GameRecordAdapter extends BaseAdapter {
    private List<GameRecord> list;
    private static final String[] colorArr={"#F6A915","#7D7C7C","#F46E12"};//冠军、亚军、季军的颜色值

    public GameRecordAdapter(List<GameRecord> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.leaderboard_item,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.scoreTv=convertView.findViewById(R.id.scoreTv);
            viewHolder.playerNameTv=convertView.findViewById(R.id.playerNameTv);
            viewHolder.levelTv=convertView.findViewById(R.id.levelTv);
            viewHolder.spendTimeTv=convertView.findViewById(R.id.spendTimeTv);
            viewHolder.timeTv=convertView.findViewById(R.id.timeTv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        GameRecord gameRecord = list.get(position);
        if(position>2){
            viewHolder.scoreTv.setVisibility(View.GONE);
        }else{
            viewHolder.scoreTv.setVisibility(View.VISIBLE);
            if(position==0){
                viewHolder.scoreTv.setTextColor(Color.parseColor(colorArr[0]));
                viewHolder.scoreTv.setText("1st");
            }else if(position==1){
                viewHolder.scoreTv.setTextColor(Color.parseColor(colorArr[1]));
                viewHolder.scoreTv.setText("2nd");
            }else if(position==2){
                viewHolder.scoreTv.setTextColor(Color.parseColor(colorArr[2]));
                viewHolder.scoreTv.setText("3th");
            }
        }
        viewHolder.playerNameTv.setText(gameRecord.getPlayerName());
        viewHolder.levelTv.setText(gameRecord.getLevel()+"关");
        int min= (int) (gameRecord.getSpendTime()/(1000*60));//分钟
        int second= (int) (gameRecord.getSpendTime()-(min*60*1000))/1000;//秒
        viewHolder.spendTimeTv.setText(min+"分"+second+"秒");
        viewHolder.timeTv.setText(DateUtils.formatDateByTimeMillis(gameRecord.getTimeMillis()));
        return convertView;
    }

    static class ViewHolder{
        TextView scoreTv;
        TextView playerNameTv;//玩家名称
        TextView levelTv;//闯到了第几关
        TextView spendTimeTv;//整个游戏过程所花费的时间
        TextView timeTv;//游戏结束时的时间
    }
}
